package com.lyf.network.learn.nio.selector;

import org.junit.Test;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
import java.util.Set;

/**
 * @AUTHOR LYF
 * @DATE 2022/6/10
 * @VERSION 1.0
 * @DESC
 * 1.Selector 与 channel的关系
 * 2.selector 的注册方式和四种状态（OP_READ，WRITE，RECEIVE）--- 监听的就绪状态
 * 3.fileChannel不可注册selector,Socket...可
 * 4.选择键
 *
 */
public class SelectorConcept {
    @Test
    public void test1() throws Exception{
        // 1.创建
        Selector selector = Selector.open();

        // 2.注册
        ServerSocketChannel socketChannel = ServerSocketChannel.open();
        //socketChannel.accept().bind(new InetSocketAddress(9999));
        socketChannel.configureBlocking(false);
        socketChannel.bind(new InetSocketAddress(9999));
        //SocketChannel socketChannel1 = socketChannel.accept();
        socketChannel.accept();
        socketChannel.register(selector, SelectionKey.OP_ACCEPT);
        //socketChannel1.register(selector,SelectionKey.OP_ACCEPT);

        ServerSocketChannel socketChannel1 = ServerSocketChannel.open();
        socketChannel1.configureBlocking(false);
        socketChannel1.bind(new InetSocketAddress(9998));
        socketChannel1.accept();
        socketChannel1.register(selector, SelectionKey.OP_ACCEPT);


        // 3.
        Set<SelectionKey> selectionKeys = selector.selectedKeys();

        while (true){//true
            int i = selector.select();//--
            System.out.println("监听..."+i);

            for(SelectionKey selectionKey:selectionKeys){
                System.out.println(selectionKey);
                if(selectionKey.isAcceptable()){
                    //
                    System.out.println("accept..");
                }else if(selectionKey.isConnectable()){

                }else if(selectionKey.isWritable()){
                    System.out.println("write..");

                }
            }
            Thread.sleep(2000);
        }


    }
    @Test
    public void client() throws Exception{


        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(9999));

        socketChannel.configureBlocking(false);

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        while (true){

            String str = new Scanner(System.in).nextLine();
            buffer.put(str.getBytes());
            buffer.flip();
            socketChannel.write(buffer);
            buffer.clear();

        }


       // socketChannel.close();
    }

    public static void main(String[] args) throws Exception{

        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(9999));

        socketChannel.configureBlocking(false);

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        while (true){

            String str = new Scanner(System.in).nextLine();
            buffer.put(str.getBytes());
            buffer.flip();
            socketChannel.write(buffer);
            buffer.clear();
        }


    }

}
